package com.sskj.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sskj.api.domain.Permission;
import com.sskj.api.domain.RolePermission;
import com.sskj.api.mapper.RolePermissionMapper;
import com.sskj.api.service.IPermissionService;
import com.sskj.api.service.IRolePermissionService;
import com.sskj.common.dto.TreeNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.*;

@Slf4j
@Service
public class RolePermissionService extends ServiceImpl<RolePermissionMapper, RolePermission> implements IRolePermissionService {

    @Autowired
    private IPermissionService permissionService;

    @Transactional
    @Override
    public void saveOrUpdate(Long roleId, List<Long> permissionIds) {
        LambdaQueryWrapper<RolePermission> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(RolePermission::getRoleId, roleId);
        remove(lambdaQueryWrapper);

        if (permissionIds != null && !permissionIds.isEmpty()) {
            List<RolePermission> rolePermissions = new ArrayList<>();
            for (Long permissionId : permissionIds) {
                RolePermission rolePermission = new RolePermission();
                rolePermission.setPermissionId(permissionId);
                rolePermission.setRoleId(roleId);
                rolePermission.setCreateTime(LocalDateTime.now());

                rolePermissions.add(rolePermission);
            }

            saveBatch(rolePermissions);
        }
    }

    @Override
    public Set<Long> listPermissionByRoleIds(List<Long> roleIds) {
        LambdaQueryWrapper<RolePermission> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(RolePermission::getRoleId, roleIds);
        List<RolePermission> list = list(lambdaQueryWrapper);
        if (list != null && !list.isEmpty()) {
            Set<Long> permissionIds = new HashSet<>();
            for (RolePermission rolePermission : list) {
                permissionIds.add(rolePermission.getPermissionId());
            }

            return permissionIds;
        }
        return Set.of();
    }

    @Override
    public List<TreeNode<Permission>> treeByRoleIds(List<Long> roleIds) {
        if (roleIds != null && !roleIds.isEmpty()) {
            LambdaQueryWrapper<RolePermission> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.in(RolePermission::getRoleId, roleIds);
            List<RolePermission> rolePermissions = list(lambdaQueryWrapper);

            if (rolePermissions != null && !roleIds.isEmpty()) {
                Set<Long> permissionIds = new HashSet<>();
                for (RolePermission rolePermission : rolePermissions) {
                    permissionIds.add(rolePermission.getPermissionId());
                }

                return permissionService.tree(permissionIds, -1);
            }

        }
        return null;
    }
}
